// wtkrootwidget.cpp
//

#include "wtk/wtkrootwidget.hpp"

using namespace wtk;



// RootLayout::set_root_size
//
void RootLayout::set_root_size ( int width,
                                 int height )
{
  this->width = width;
  this->height = height;
}



// RootLayout::process_resize
//
void RootLayout::process_resize ()
{
  WTK_TRACE("process_resize");

  for (auto &it: children_layout)
    {
      Widget *child = it.first;
      ChildLayout &layout = it.second;
      if (child->needs_resize())
        {
          WTK_TRACE("child: %s", WGL_DEBUG_CNAME(child));
          SizeRequest req;
          child->size_request(req);
          if (layout.fullscreen)
            {
              child->size_allocate({0, 0, width, height});
            }
          else
            {
              child->size_allocate({4, 4, req.width, req.height});
            }
        }
    }
}



// RootLayout::add_child
//
void RootLayout::add_child ( Widget &child )
{
  children_layout.emplace(&child, ChildLayout{child});
}



// RootLayout::remove_child
//
void RootLayout::remove_child ( Widget &child )
{
  children_layout.erase(&child);
}



// RootLayout::set_child_properties
//
void RootLayout::set_child_properties ( Widget &child,
                                           bool fullscreen )
{
  auto it = children_layout.find(&child);
  if (it == children_layout.end())
    {
      WTK_ERROR("RootLayout: widget is not registered");
      return;
    }
  ChildLayout &layout = it->second;
  layout.fullscreen = fullscreen;
}



// RootLayout::clear
//
void RootLayout::clear ()
{
  children_layout.clear();
}



// RootLayout::size_request
//
void RootLayout::size_request ( SizeRequest &req )
{
  WTK_TRACE("RootLayout.size_request()");
  // for (auto &it: children_layout)
  //   {
  //     ChildLayout &layout = it.second;
  //     layout.child.size_request(layout.request);
  //     WTK_TRACE(" %s -> {%d, %d}",
  //               WGL_DEBUG_CNAME(&layout.child),
  //               layout.request.width,
  //               layout.request.height);
  //   }
}



// RootLayout::size_allocate
//
void RootLayout::size_allocate ( const Allocation &alloc )
{
  WTK_TRACE("RootLayout.size_allocate()");
  // for (auto &it: children_layout)
  //   {
  //     ChildLayout &layout = it.second;

  //     Allocation child_alloc;
  //     if (layout.fullscreen)
  //       {
  //         child_alloc = {8, 8, width-16, height-16};
  //       }
  //     else
  //       {
  //         child_alloc = {64, 64, layout.request.width, layout.request.height};
  //       }
  //     WTK_TRACE("  %s -> {%d, %d, %d, %d}",
  //               WGL_DEBUG_CNAME(&layout.child),
  //               child_alloc.x,
  //               child_alloc.y,
  //               child_alloc.width,
  //               child_alloc.height);
  //     layout.child.size_allocate(child_alloc);
  //   }
}



// RootWidget::RootWidget
//
RootWidget::RootWidget ( Display &display )
  : Widget(nullptr),
    display(display)
{
}



// RootWidget::process_redraw
//
void RootWidget::process_redraw ()
{
  // [TEMP] reset context
  gl.UseProgram(0);
  gl.Disable(GL_DEPTH_TEST);
  gl.BindTexture(GL_TEXTURE_2D, 0);
  gl.Disable(GL_TEXTURE_2D);
  gl.PolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  gl.ClearColor(1.0f, 1.0f, 0.5f, 0.0f);
  gl.Clear(GL_COLOR_BUFFER_BIT);

  DrawContext context{display, 0, 0, width, height};
  draw_child(context);
}



// RootWidget::draw
//
void RootWidget::draw ( const DrawContext &context )
{
}
